package ma.platform.server.student.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import ma.plaform.server.common.log.annotation.SysLog;
import ma.platform.server.core.exception.CheckedException;
import ma.platform.server.core.util.R;
import ma.platform.server.student.entity.TbClasses;
import ma.platform.server.student.entity.TbGrade;
import ma.platform.server.student.entity.TbGrade;
import ma.platform.server.student.service.TbClassesService;
import ma.platform.server.student.service.TbGradeService;
import ma.platform.server.student.service.TbGradeService;
import org.apache.commons.collections.CollectionUtils;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/grade")
@Tag(description = "grade", name = "年级管理模块")
public class GradeController {

    @Resource
    private TbGradeService gradeService;

    @Resource
    private TbClassesService classesService;

    @GetMapping("/page")
    public R page(@ParameterObject Page page, @ParameterObject TbGrade grade) {
        return R.ok(gradeService.page(page, Wrappers.<TbGrade>lambdaQuery()
                .like(StrUtil.isNotBlank(grade.getGradeName()), TbGrade::getGradeName, grade.getGradeName())
                .eq(Objects.nonNull(grade.getSchoolId()), TbGrade::getSchoolId, grade.getSchoolId()).orderByAsc(TbGrade::getSortd)));
    }

    @GetMapping("/list")
    public R list(@ParameterObject TbGrade grade) {
        return R.ok(gradeService.list(Wrappers.<TbGrade>lambdaQuery()
                .like(StrUtil.isNotBlank(grade.getGradeName()), TbGrade::getGradeName, grade.getGradeName())
                .eq(Objects.nonNull(grade.getSchoolId()), TbGrade::getSchoolId, grade.getSchoolId()).orderByAsc(TbGrade::getSortd)));
    }

    /**
     * 添加学生
     *
     * @return success、false
     */
    @SysLog("添加年级")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('tb_grade_add')")
    public R save(@Valid @RequestBody TbGrade grade) {
        gradeService.save(grade);
        return R.ok(grade);
    }

    /**
     * 删除学生
     *
     * @param ids ID
     * @return R
     */
    @SysLog("删除年级")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('tb_grade_del')")
    public R removeById(@RequestBody Long[] ids) {
        List<TbClasses> classesList = classesService.list(Wrappers.<TbClasses>lambdaQuery().in(TbClasses::getSchoolId, ids));
        if (CollectionUtils.isNotEmpty(classesList)){
            throw new CheckedException("该学校下有班级，无法删除");
        }
        return R.ok(gradeService.removeByIds(Lists.newArrayList(ids)));
    }

    /**
     * 修改学生
     *
     * @return success/false
     */
    @PutMapping
    @SysLog("修改年级")
    @PreAuthorize("@pms.hasPermission('tb_grade_edit')")
    public R updateById(@Valid @RequestBody TbGrade grade) {
        gradeService.updateById(grade);
        return R.ok();
    }

    /**
     * 通过ID查询信息
     * @param id ID
     */
    @GetMapping("/details/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok(gradeService.getById(id));
    }
}
